﻿using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for AuthorViewEditor.xaml
    /// </summary>
    public partial class AuthorEditorView
    {
        private readonly AuthorController _authorControl;
        private readonly Author _author;

        public AuthorEditorView()
        {
            InitializeComponent();

            _authorControl = new AuthorController();
            Title = "Add Author";
        }

        public AuthorEditorView(Author author)
        {
            InitializeComponent();
            _authorControl = new AuthorController();
            Title = "Edit Author";

            _author = author;

            textBoxId.Text = author.Id.ToString();
            textBoxName.Text = author.Name;
        }

        private void ClearTextBox()
        {
            textBoxId.Text = "";
            textBoxName.Text = "";
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxName.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else
            {
                if (Title.Equals("Add Author"))
                {
                    var author = new Author
                                     {
                                         Name = textBoxName.Text
                                     };

                    var message = _authorControl.AddAuthor(author);

                    switch (message)
                    {
                        case 1:
                            MessageBox.Show("sukses menambah author");
                            ClearTextBox();
                            break;
                        default:
                            MessageBox.Show("nama sudah ada");
                            break;
                    }
                }
                else
                {
                    var newAuthor = new Author
                                        {
                                            Id = _author.Id,
                                            Name = textBoxName.Text
                                        };

                    var message = _authorControl.EditAuthor(newAuthor, _author.Name);

                    switch (message)
                    {
                        case 1:
                            MessageBox.Show("sukses mengedit author");
                            break;
                        default:
                            MessageBox.Show("nama sudah ada");
                            break;
                    }
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
